﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.IO;
using System.Web.Caching;

namespace KPIS.GERP.GIMS.APP.APP_UI.UserControl
{
    public partial class ICM_Master_Resource_Item : System.Web.UI.UserControl
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLResourceMasterItem bll = new ResourceMasterItemBLL();
        public static ResourceMasterItemInfo itemInfo = new ResourceMasterItemInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadUnit();
                //LoadSourceCombo();
                //LoadDataGrid();
            }
        }

        public void SetHeader(string header)
        {
            lblHeaderList.Text = "รายการพัสดุภายใต้กลุ่ม <" + header + ">";
        }

        public void LoadDataGrid()
        {
            int sub_group_seq = Convert.ToInt32(Session["SubGroupSeq"]);
            IList<ResourceMasterItemInfo> info = bll.GetResourceMasterItemWithParent(sub_group_seq);
            Grid.DataSource = info;
            Grid.DataBind();

            cmdFac.GridCommand(panelList, panelGrid, panelCommand);
        }

        private void LoadUnit()
        {
            IBLLMasterUnit unit = new MasterUnitBLL();
            IList<MasterUnitInfo> info = unit.GetMasterUnit();
            cmbUnit.DataSource = info;
            cmbUnit.DataBind();
        }

        public void GetKeyValue(string seq)
        {
            if (seq.Length > 0)
            {
                itemInfo = bll.GetResourceMasterItem(Convert.ToInt32(seq));

                txtCode.Text = itemInfo.Code;
                txtName.Text = itemInfo.Name;
                txtDescr.Text = itemInfo.Descr;
                cmbUnit.SelectedValue = itemInfo.InfoUnit.Seq.ToString();
                if (cmbUnit.SelectedValue == "")
                {
                    cmbUnit.SelectedIndex = -1;
                    cmbUnit.Text = "";
                }
                txtGiinCode.Text = itemInfo.Giin_code;
                txtGpscCode.Text = itemInfo.Gpsc_code;
                txtStdPrice.Text = itemInfo.Std_price.ToString();
                txtMaxPrice.Text = itemInfo.Max_price.ToString();
                txtMinQty.Text = itemInfo.Min_qty.ToString();
                txtMaxQty.Text = itemInfo.Max_qty.ToString();
                imgPhoto.Dispose();
                imgPhoto.ImageUrl = (itemInfo.Image_name.Length > 0
                    ? SystemInfo._ImagePath + itemInfo.Image_name + "?n=" + DateTime.Now.ToString()
                    : SystemInfo._EmptyImage + "?n=" + DateTime.Now.ToString());

                cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
                PanelPhoto.Visible = false;
                ((HiddenField)this.Parent.FindControl("hdChangeFile")).Value = "";
            }
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList, panelGrid, panelCommand);
            PanelPhoto.Visible = true;
            imgPhoto.ImageUrl = SystemInfo._EmptyImage;
            ((HiddenField)this.Parent.FindControl("hdChangeFile")).Value = "";
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            cmdFac.EditCommand(panelList, panelGrid, panelCommand);
            PanelPhoto.Visible = true;
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            IList<ResourceMasterItemInfo> infoList = new List<ResourceMasterItemInfo>();
            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in Grid.MasterTableView.GetSelectedItems())
                {
                    ResourceMasterItemInfo info = new ResourceMasterItemInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    info.Seq = Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Seq"]);
                    infoRecord.Updated_by = Convert.ToInt32(Session["EmpSeq"].ToString());
                    infoRecord.Updated_when = DateTime.Now;
                    info.InfoRecord = infoRecord;
                    infoList.Add(info);
                }
            }
            else
            {
                ResourceMasterItemInfo info = new ResourceMasterItemInfo();
                RecordInfo infoRecord = new RecordInfo();
                info.Seq = Convert.ToInt32(((HiddenField)this.Parent.FindControl("hdSeq")).Value);
                infoRecord.Updated_by = Convert.ToInt32(Session["EmpSeq"].ToString());
                infoRecord.Updated_when = DateTime.Now;
                info.InfoRecord = infoRecord;
                infoList.Add(info);
            }

            if (infoList.Count == 0)
            {
                ((RadAjaxManager)this.Parent.FindControl("RadManager1")).Alert(SystemInfo._DeleteNull);
                return;
            }

            try
            {
                bll.DeleteAll(infoList);
                ((Form.ICM_Master_Resource)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent).RefreshTree();
            }
            catch
            {
                ((RadAjaxManager)this.Parent.FindControl("RadManager1")).Alert(SystemInfo._DeleteError);
                return;
            }

            LoadDataGrid();
        }

        protected void AddNewSave_Click(object sender, EventArgs e)
        {
            ResourceMasterItemInfo info = new ResourceMasterItemInfo();
            MasterUnitInfo infoUnit = new MasterUnitInfo();
            ImageInfo infoImage = new ImageInfo();
            ResourceMasterSubGroupInfo infoSubGroup = new ResourceMasterSubGroupInfo();
            RecordInfo infoRecord = new RecordInfo();
            string source_file = ((HiddenField)this.Parent.FindControl("hdSourceFile")).Value;
            if (source_file.Length > 0)
            {
                FileInfo fiSource = new FileInfo(source_file);
                infoImage.File_info = fiSource;
            }
            int sub_group_seq = Convert.ToInt32(Session["SubGroupSeq"]);
            info.Code = txtCode.Text;
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            infoUnit.Seq = (cmbUnit.SelectedValue.Length == 0 ? null : (Nullable<int>)Convert.ToInt32(cmbUnit.SelectedValue));
            infoSubGroup.Seq = sub_group_seq;
            info.Giin_code = txtGiinCode.Text;
            info.Gpsc_code = txtGpscCode.Text;
            info.Std_price = (txtStdPrice.Text.Length == 0 ? null : (Nullable<double>)Convert.ToDouble(txtStdPrice.Text));
            info.Max_price = (txtMaxPrice.Text.Length == 0 ? null : (Nullable<double>)Convert.ToDouble(txtMaxPrice.Text));
            info.Min_qty = (txtMinQty.Text.Length == 0 ? null : (Nullable<int>)Convert.ToDouble(txtMinQty.Text));
            info.Max_qty = (txtMaxQty.Text.Length == 0 ? null : (Nullable<int>)Convert.ToDouble(txtMaxQty.Text));
            infoImage.Dest_path = Server.MapPath(SystemInfo._ImagePath);
            infoRecord.Created_by = cn.UserID;
            infoRecord.Created_when = DateTime.Now;
            info.InfoUnit = infoUnit;
            info.InfoSubGroup = infoSubGroup;
            info.InfoImage = infoImage;
            info.InfoRecord = infoRecord;

            if (ValtxtCode.IsValid == false || ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                bll.Insert(info);
                ((Form.ICM_Master_Resource)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent).RefreshTree();
            }
            catch
            {
                ((RadAjaxManager)this.Parent.FindControl("RadManager1")).Alert(SystemInfo._InsertError);
                return;
            }

            LoadDataGrid();
        }

        protected void EditSave_Click(object sender, EventArgs e)
        {
            ResourceMasterItemInfo info = new ResourceMasterItemInfo();
            MasterUnitInfo infoUnit = new MasterUnitInfo();
            ImageInfo infoImage = new ImageInfo();
            ResourceMasterSubGroupInfo infoSubGroup = new ResourceMasterSubGroupInfo();
            RecordInfo infoRecord = new RecordInfo();
            string source_file = ((HiddenField)this.Parent.FindControl("hdSourceFile")).Value;
            string change_file = ((HiddenField)this.Parent.FindControl("hdChangeFile")).Value;
            if (source_file.Length > 0)
            {
                FileInfo fiSource = new FileInfo(source_file);
                infoImage.File_info = fiSource;
            }
            int sub_group_seq = Convert.ToInt32(Session["SubGroupSeq"]);
            info.Seq = Convert.ToInt32(((HiddenField)this.Parent.FindControl("hdSeq")).Value);
            info.Code = txtCode.Text;
            info.Name = txtName.Text;
            info.Descr = txtDescr.Text;
            infoUnit.Seq = (cmbUnit.SelectedValue.Length == 0 ? null : (Nullable<int>)Convert.ToInt32(cmbUnit.SelectedValue));
            infoSubGroup.Seq = sub_group_seq;
            info.Giin_code = txtGiinCode.Text;
            info.Gpsc_code = txtGpscCode.Text;
            info.Std_price = (txtStdPrice.Text.Length == 0 ? null : (Nullable<double>)Convert.ToDouble(txtStdPrice.Text));
            info.Max_price = (txtMaxPrice.Text.Length == 0 ? null : (Nullable<double>)Convert.ToDouble(txtMaxPrice.Text));
            info.Min_qty = (txtMinQty.Text.Length == 0 ? null : (Nullable<int>)Convert.ToDouble(txtMinQty.Text));
            info.Max_qty = (txtMaxQty.Text.Length == 0 ? null : (Nullable<int>)Convert.ToDouble(txtMaxQty.Text));
            infoImage.Dest_path = Server.MapPath(SystemInfo._ImagePath);
            info.InfoImage = infoImage;
            if (info.InfoImage.File_info != null)
                info.Image_name = info.Seq.ToString() + info.InfoImage.File_info.Extension;
            else
                info.Image_name = "";

            infoRecord.Updated_by = Convert.ToInt32(Session["EmpSeq"].ToString());
            infoRecord.Updated_when = DateTime.Now;
            info.InfoUnit = infoUnit;
            info.InfoSubGroup = infoSubGroup;
            info.InfoRecord = infoRecord;

            if (ValtxtCode.IsValid == false || ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                bll.Update(info, change_file);
                ((Form.ICM_Master_Resource)this.Parent.Parent.Parent.Parent.Parent.Parent.Parent).RefreshTree();
            }
            catch
            {
                ((RadAjaxManager)this.Parent.FindControl("RadManager1")).Alert(SystemInfo._InsertError);
                return;
            }
            cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
            PanelPhoto.Visible = false;
            if (info.Image_name.Length > 0)
            {
                imgPhoto.ImageUrl = (info.Image_name.Length > 0
                        ? SystemInfo._ImagePath + info.Image_name + "?n=" + DateTime.Now.ToString()
                        : SystemInfo._EmptyImage + "?n=" + DateTime.Now.ToString());
            }
            else
            {
                imgPhoto.ImageUrl = (itemInfo.Image_name.Length > 0
                        ? SystemInfo._ImagePath + itemInfo.Image_name + "?n=" + DateTime.Now.ToString()
                        : SystemInfo._EmptyImage + "?n=" + DateTime.Now.ToString());
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            cmdFac.CancelCommand(panelList, panelGrid, panelCommand);
            PanelPhoto.Visible = false;
        }

        protected void ShowGrid_Click(object sender, EventArgs e)
        {
            LoadDataGrid();
        }

        protected void Grid_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadDataGrid();
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in Grid.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            Grid.MasterTableView.FilterExpression = string.Empty;
            LoadDataGrid();
        }
    }
}